<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Creating a new Apache Camel JUnit test case</title><link rel="stylesheet" type="text/css" href="eclipse_book.css"><meta name="generator" content="DocBook XSL Stylesheets V1.77.1"><link rel="home" href="index.html" title="Red Hat Fuse Tooling"><link rel="up" href="RiderDevPart.html" title="Developing Applications"><link rel="prev" href="RiderDevPart.html" title="Developing Applications"><link rel="next" href="RiderCamelDebugger.html" title="Debugging Routing Contexts"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="newCamelTestCase"></a>Creating a new Apache Camel JUnit test case</h2></div></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="_overview_9"></a>Overview</h3></div></div></div><p>A common way of testing routes is to use JUnit. The design time tooling includes a
wizard that simplifies creating a JUnit test case for your routes. The wizard uses the
endpoints you specify to generate the starting point code and configuration for the
test.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="imagesdb/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>After you create the boilerplate JUnit test case, you need to modify it to add
expectations and assertions specific to the route that you&#8217;ve created or modified,
so the test is valid for the route.</p></td></tr></table></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="_prerequisites_10"></a>Prerequisites</h3></div></div></div><p>Before you create a new JUnit test case, you need to perform a preliminary
task:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">If you are replacing an existing JUnit test case, you need to delete it before you create a
new one. See <a class="xref" href="newCamelTestCase.html#ugDeleteJUnitFile" title="Deleting and existing JUnit test case">Deleting and existing JUnit test case</a>.</li><li class="listitem">If you are creating a new JUnit test case in a project that hasn&#8217;t one, you need to first
create the <span class="emphasis"><em>project_root</em></span><code class="literal">/src/test/java</code>
folder for the test case that is included in the build path. See <a class="xref" href="newCamelTestCase.html#ugAddTestJavaFolder" title="Creating and adding the src/test/java folder to the build path">Creating and adding the <code class="literal">src/test/java</code> folder to the build path</a>.</li></ul></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="ugDeleteJUnitFile"></a>Deleting and existing JUnit test case</h3></div></div></div><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">In the <span class="strong"><strong>Project Explorer</strong></span> view, expand the project&#8217;s root
node to expose the
<span class="emphasis"><em>&lt;root_project&gt;</em></span><code class="literal">/src/test/java</code>
folder.</li><li class="listitem"><p class="simpara">Locate the JUnit test case file in the <code class="literal">/src/test/java</code>
folder.</p><p class="simpara">Depending on which DSL the project is based on, the JUnit test case file is
named <code class="literal">BlueprintXmlTest.java</code> or
<code class="literal">CamelContextXmlTest.java</code>.</p></li><li class="listitem"><p class="simpara">Right-click the JUnit test case <code class="literal">.java</code> file to open the context menu, and then
select <span class="strong"><strong>Delete</strong></span>.</p><p class="simpara">The JUnit test case <code class="literal">.java</code> file disappears from the
<span class="strong"><strong>Project Explorer</strong></span> view.</p><p class="simpara">You can now <a class="link" href="newCamelTestCase.html#ugCreateJUnitTest" title="Creating a JUnit test case">create a new JUnit test case</a>.</p></li></ol></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="ugAddTestJavaFolder"></a>Creating and adding the <code class="literal">src/test/java</code> folder to the build path</h3></div></div></div><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">In the <span class="strong"><strong>Project Explorer</strong></span> view, right-click the project&#8217;s
root to open the context menu.</li><li class="listitem">Select <span class="strong"><strong>New</strong></span> &#8594; <span class="strong"><strong>Folder</strong></span> to open the <span class="strong"><strong>Create a new folder resource</strong></span> wizard.</li><li class="listitem"><p class="simpara">In the wizard&#8217;s project tree pane, expand the project&#8217;s root node and select
the <code class="literal">src</code> folder.</p><p class="simpara">Make sure <span class="emphasis"><em>&lt;project_root&gt;</em></span><code class="literal">/src</code> appears in the <span class="strong"><strong>Enter or
select the parent folder</strong></span>
field.</p></li><li class="listitem">In <span class="strong"><strong>Folder name</strong></span>, enter <code class="literal"><span class="strong"><strong>/test/java</strong></span></code>.
This folder will store the new JUnit test case you create.</li><li class="listitem"><p class="simpara">Click <span class="strong"><strong>Finish</strong></span>.</p><p class="simpara">In the <span class="strong"><strong>Project Explorer</strong></span> view, the new
<code class="literal">src/test/java</code> folder appears under the
<code class="literal">src/main/resources</code> folder. You can verify that this
folder is on the class path by opening its context menu and selecting
<span class="strong"><strong>Build Path</strong></span>. If <span class="strong"><strong>Remove from Build
Path</strong></span> is a menu option, you know the
<code class="literal">src/test/java</code> folder is on the class path.</p><p class="simpara">You can now <a class="link" href="newCamelTestCase.html#ugCreateJUnitTest" title="Creating a JUnit test case">create a new JUnit test case</a>.</p></li></ol></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="ugCreateJUnitTest"></a>Creating a JUnit test case</h3></div></div></div><p>To create a new JUnit test case for your route:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">In the <span class="strong"><strong>Project Explorer</strong></span> view, select the routing context
<code class="literal">.xml</code> file in your project.</li><li class="listitem"><p class="simpara">Right-click it to open the context menu, and then select <span class="strong"><strong>New</strong></span> &#8594; <span class="strong"><strong>Camel Test Case</strong></span> to open the <span class="strong"><strong>New Camel JUnit Test Case</strong></span> wizard,
as shown in <a class="xref" href="newCamelTestCase.html#CmlTestCaseWiz" title="Figure&nbsp;5.&nbsp;New Camel JUnit Test Case wizard">Figure&nbsp;5, &#8220;New Camel JUnit Test Case wizard&#8221;</a>.</p><div class="figure"><a name="CmlTestCaseWiz"></a><p class="title"><b>Figure&nbsp;5.&nbsp;New Camel JUnit Test Case wizard</b></p><div class="figure-contents"><div class="mediaobject"><img src="./images/newCamTstCase.png" alt="Entry page of the New Camel JUnit Test Case wizard"></div></div></div><br class="figure-break"><p class="simpara">Alternatively, you can open the wizard by selecting <span class="strong"><strong>File</strong></span> &#8594; <span class="strong"><strong>New</strong></span> &#8594; <span class="strong"><strong>Other</strong></span> &gt; <span class="strong"><strong>Fuse</strong></span> &gt; <span class="strong"><strong>Camel Test Case</strong></span> from the menu bar.</p></li><li class="listitem"><p class="simpara">In <span class="strong"><strong>Source folder</strong></span>, accept the default location of the
source code for the test case, or enter another location.</p><p class="simpara">You can click
<span class="inlinemediaobject"><img src="./images/BrowseButton.jpg" alt="Browse button"></span> to search for a location.</p></li><li class="listitem"><p class="simpara">In <span class="strong"><strong>Package</strong></span>, accept the default package name for the
generated test code, or enter another package name.</p><p class="simpara">You can click
<span class="inlinemediaobject"><img src="./images/BrowseButton.jpg" alt="Browse button"></span> to search for a package.</p></li><li class="listitem"><p class="simpara">In <span class="strong"><strong>Camel XML file under test</strong></span>, accept the default pathname
of the routing context file that contains the route you want to test, or enter
another pathname.</p><p class="simpara">You can click
<span class="inlinemediaobject"><img src="./images/BrowseButton.jpg" alt="Browse button"></span> to search for a context file.</p></li><li class="listitem">In <span class="strong"><strong>Name</strong></span>, accept the default name for the generated test
class, or enter another name.</li><li class="listitem">Select the method stubs you want to include in the generated code.</li><li class="listitem">If you want to include the default generated comments in the generated code,
check the <span class="strong"><strong>Generate comments</strong></span> box.</li><li class="listitem"><p class="simpara">Click
<span class="inlinemediaobject"><img src="./images/NextButton.jpg" alt="Next button"></span> to open the  <span class="strong"><strong>Test Endpoints</strong></span> page. For
example, <a class="xref" href="newCamelTestCase.html#CmlTestEndpts" title="Figure&nbsp;6.&nbsp;New Camel JUnit Test Case page">Figure&nbsp;6, &#8220;New Camel JUnit Test Case page&#8221;</a> shows a route&#8217;s input and output file
endpoints selected.</p><div class="figure"><a name="CmlTestEndpts"></a><p class="title"><b>Figure&nbsp;6.&nbsp;New Camel JUnit Test Case page</b></p><div class="figure-contents"><div class="mediaobject"><img src="./images/newCamTstEndpts.png" alt="Example Test Endpoints page"></div></div></div><br class="figure-break"></li><li class="listitem">Under <span class="strong"><strong>Available endpoints</strong></span>, select the endpoints you want
to test. Click the checkbox next to any selected endpoint to deselect it.</li><li class="listitem"><p class="simpara">Click
<span class="inlinemediaobject"><img src="./images/FinishButton.jpg" alt="Finish button"></span>.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="imagesdb/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>If prompted, add JUnit to the build path.</p></td></tr></table></div></li></ol></div><p>The artifacts for the test are added to your project and appear in the
<span class="strong"><strong>Project Explorer</strong></span> view under <code class="literal">src/test/java</code>. The
class implementing the test case opens in the Java editor.</p></div></div></body></html>